package net.hctek.identity.service.impl;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.hctek.identity.bean.Files;
import net.hctek.identity.bean.Navs;
import net.hctek.identity.bean.User;
import net.hctek.identity.dao.impl.FilesDao;
import net.hctek.identity.dao.impl.NavsDao;
import net.hctek.identity.dao.impl.UserDao;
import net.hctek.identity.service.IdentityServiceI;
import net.hctek.identity.util.MD5;


/** 
* @author Ken 384755534@qq.com: 
* @version 创建时间：2017年2月20日 下午3:13:58 
* 类说明 :
*/
@Service
@Transactional(readOnly=false,rollbackFor=java.lang.Exception.class)
public class IdentityService implements IdentityServiceI{

	@Autowired
	private UserDao userDao;
	@Autowired
	private FilesDao filesDao;
	@Autowired
	private NavsDao navsDao;
	
	
	//根据账号ID 获取用户的信息
	@Transactional(readOnly=true)
	@Override
	public User getUserById(String userId) {
		try {
				return userDao.get(User.class, userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//添加用户
	@Override
	public void savaUser(User user) {
		try {
			userDao.save(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//添加文件
	@Override
	public void savaFiles(List<Files> files) {
		try {
			filesDao.saveAll(files);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//校验文件名是否重复
	@Override
	public List<String> Duplicatefiles(String userId,String uploadFileName) {
		try {
			return filesDao.selectFilesByName(userId,uploadFileName);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("校验异常");
		}
		return null;
	}

	//用户文件是否重复
	@Override
	public void repeat(String id, String userid) {
		try {
			if(filesDao.selectMiddleById(id, userid).toString().equals("[]")){
				filesDao.inserMiddleById(id, userid);
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("插入数据错误");
		}
	}
	
	//根据UserID查找对应的路径
	@Override
	public List<Files> selectFilePath(String userid) {
		try {
			return filesDao.selectFilePathById(userid);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("查找失败");
		}
		return null;
	}
	//查找所有的用户名
	@Override
	public List<String> getUserNameAll() {
		try {
			return userDao.getUserByIdAll();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("查找所有的用户名失败");
		}
		return null;
	}
	//查找所有的用户信息
	@Override
	public List<User> selectUser() {
		try {
			return userDao.getUserAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	//修改的用户信息
	@Override
	public String updateUser(User user) {
		try {
			User u = userDao.get(User.class, user.getUserId());
			if(user.getPassWord().equals(u.getPassWord())){
				u.setPassWord(user.getPassWord());
			}else{
				String passwordx = MD5.EncoderByMd5(user.getPassWord());
				u.setPassWord(passwordx);
			}
			u.setDelFlag("1");
			u.setUserLanguage(user.getUserLanguage());
			u.setUserMailBox(user.getUserMailBox());
			u.setUserPhoneNumBer(user.getUserPhoneNumBer());
			u.setUserRace(user.getUserRace());
			u.setUserSex(user.getUserSex());
			return "修改成功";
		} catch (Exception e) {
			e.printStackTrace();
			return "修改失败";
		}
	}
	//删除用户
	@Override
	public void deleteUserByIds(String userId) {
		try {
			User u = userDao.get(User.class, userId);
			u.setDelFlag("0");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	//用户是否重复
	@Override
	public boolean ajaxValidName(String userId) {
		try {
			User u = userDao.get(User.class, userId);
			if(u!=null){
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	

	@Override
	public boolean repeatfiles(String userId, String string) {
			try {
				List<Files> files = filesDao.repeatfile(userId,string);
				System.out.println("files.toString():"+files.toString());
				if(files.toString().equals("[]")){
					return true;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		return false;
	}

	@Override
	public List<String> filesAll() {
		try {
			return filesDao.getAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Navs> getNavsAll() {
		try {
			return navsDao.getAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

/*	//复原用户
	@Override
	public void recovery(User user) {
		User u = userDao.get(User.class,user.getUserId());
		u.setDelFlag("1");
		u.setPassWord(user.getPassWord());
		u.setUserLanguage(user.getUserLanguage());
		u.setUserMailBox(user.getUserMailBox());
		u.setUserPhoneNumBer(user.getUserPhoneNumBer());
		u.setUserRace(user.getUserRace());
		u.setUserSex(user.getUserSex());
		
	}*/
	
}
